package com.zhangmeng.online.exam.controller;

import com.zhangmeng.online.exam.dao.SubjectDao;
import com.zhangmeng.online.exam.dto.Result;
import com.zhangmeng.online.exam.entity.Question;
import com.zhangmeng.online.exam.entity.Subject;
import com.zhangmeng.online.exam.entity.User;
import com.zhangmeng.online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zm
 * @date 2025/3/17 16:44
 * @version: 1.0
 */
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private SubjectDao subjectDao;

    @RequestMapping("/list")
    public Object list(Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(PageUtils.getPageNum(pageNum), pageSize, Sort.Direction.DESC, "addTime");
        Page<Subject> all = subjectDao.findAll(pageable);
        List<Subject> list = all.getContent();
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Subject subject : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", subject.getId());
            map.put("name", subject.getName());
            map.put("code", subject.getCode());
            map.put("description", subject.getDescription());
            map.put("addTime", subject.getAddTime());
            resultList.add(map);
        }
        return Result.success(pageNum, pageSize, all.getTotalElements(), resultList);
    }


    @RequestMapping("/getALLSubjectType")
    public Result getALLSubjectType() {

        List<Subject> all = this.subjectDao.findAll();

        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Subject subject : all) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", subject.getId());
            map.put("name", subject.getName());
            map.put("code", subject.getCode());
            map.put("desc", subject.getDescription());
//            map.put("profession",subject.getProfession().getName());
            resultList.add(map);
        }

        return Result.success(resultList);
    }

    @RequestMapping("/saveSubject")
    public Result saveSubject(@RequestBody @RequestParam Map<String, Object> params) {

        String id = (String) params.get("id");
        String name = (String) params.get("name");
        String code = (String) params.get("code");
        String description = (String) params.get("desc");

        Subject subject;
        if (id != null && !id.isEmpty()) {
            subject = this.subjectDao.findById(Long.parseLong(id)).get();
        } else {
            subject = new Subject();
        }
        subject.setName(name);
        subject.setCode(code);
        subject.setDescription(description);
        this.subjectDao.save(subject);
        return Result.success(null);
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam Long id) {
        this.subjectDao.deleteById(id);
        return Result.success(null);
    }

    @RequestMapping("/getSubjectById")
    public Result getSubjectById(@RequestParam Long id) {
        Subject subject = this.subjectDao.findById(id).get();
        Map<String, Object> map = new HashMap<>();
        map.put("id", subject.getId());
        map.put("name", subject.getName());
        map.put("code", subject.getCode());
        map.put("desc", subject.getDescription());
        return Result.success(map);
    }

    @RequestMapping("/getSubjectByCode")
    public Result getSubjectByCode(@RequestParam String code) {
        Subject subject = this.subjectDao.findByCode(code);
        Map<String, Object> map = new HashMap<>();
        map.put("id", subject.getId());
        map.put("name", subject.getName());
        map.put("code", subject.getCode());
        map.put("desc", subject.getDescription());
        return Result.success(map);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody @RequestParam Map<String, Object> params) {

        String id = params.get("id").toString();
        String fieldName = params.get("fieldName").toString();
//        String oldValue = params.get("oldValue").toString();
        String newValue = params.get("newValue").toString();

        Subject subject = subjectDao.findById(Long.parseLong(id)).get();

        switch (fieldName) {
            case "name" -> subject.setName(newValue);
            case "code" -> subject.setCode(newValue);
            case "description" -> subject.setDescription(newValue);
        }

        subjectDao.save(subject);
        return Result.success(null);
    }
}
